package com.docker.core;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.support.http.WebStatFilter;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.wall.WallFilter;
import com.docker.core.constant.ConfigConstant;
import com.docker.core.handler.APIHandler;
import com.docker.core.interceptor.CORSInterceptor;
import com.docker.core.interceptor.ExceptionInterceptor;
import com.docker.core.plugin.SysInitPlugin;
import com.docker.core.route.MainRoute;
import com.docker.login.LoginController;
import com.jfinal.config.*;
import com.jfinal.json.MixedJsonFactory;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.druid.IDruidStatViewAuth;
import com.jfinal.template.Engine;
import kit.docker.database.DataBaseConnectKit;
import kit.docker.redis.RedisConnectKit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class DockerManageConfig extends JFinalConfig {

    // 先加载开发环境配置，再追加生产环境的少量配置覆盖掉开发环境配置
    private static Prop prop = PropKit.use("docker_manage_dev.txt").appendIfExists("docker_manage_pro.txt");

    @Override
    public void configConstant(Constants constants) {
        // 开发模式
        constants.setDevMode(prop.getBoolean(ConfigConstant.SYS_DEV_MODEL, false));
        // JSON工厂
        constants.setJsonFactory(MixedJsonFactory.me());
        // AOP依赖注入(Controller、Interceptor)
        constants.setInjectDependency(true);
        // 配置 aop 代理使用 cglib，否则将使用 jfinal 默认的动态编译代理方案
        constants.setToCglibProxyFactory();
        // 添加响应参数拦截处理
//        constants.setRenderFactory(new ResponseRender());
        constants.setError404View("/common/404.html");
    }

    @Override
    public void configRoute(Routes routes) {
        routes.add("/api/login", LoginController.class);
        routes.add(new MainRoute());
    }

    @Override
    public void configEngine(Engine engine) {
    }

    @Override
    public void configPlugin(Plugins plugins) {
        DruidPlugin druid = new DruidPlugin(PropKit.get("db[1].jdbc.url"), PropKit.get("db[1].username"), PropKit.get("db[1].password"));

        // 配置防火墙加强数据库安全
        WallFilter wallFilter = new WallFilter();
        wallFilter.setDbType(JdbcConstants.POSTGRESQL);
        druid.addFilter(wallFilter);

        //配置监控
        StatFilter statFilter=new StatFilter();
        statFilter.setMergeSql(true);
        statFilter.setLogSlowSql(true);

        statFilter.setSlowSqlMillis(1500);
        druid.addFilter(statFilter);

        plugins.add(druid);

        // 数据库连接池
        DataBaseConnectKit.connect(plugins);
        // Redis连接池
        RedisConnectKit.connect(plugins);

//        plugins.add(new QuartzPlugin());

    }

    @Override
    public void configInterceptor(Interceptors interceptors) {
        interceptors.add(new ExceptionInterceptor());
        interceptors.add(new CORSInterceptor());
    }

    @Override
    public void configHandler(Handlers handlers) {
        DruidStatViewHandler dvh = new DruidStatViewHandler("/druid",//设置访问路径
                new IDruidStatViewAuth(){
                    @Override
                    public boolean isPermitted(HttpServletRequest request) {
//                         TODo 添加权限认证
                        return true ;
                    }
                });
        handlers.add(dvh);
        handlers.add(new APIHandler());
    }

    @Override
    public void onStart() {
        new SysInitPlugin().start();
    }

    @Override
    public void onStop() {
    }

}
